package com.gzhryc.shared_device.oem.h5.mng.controllers.cdcz;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.common.NumberTools;
import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.json.JsonTools;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.cdcz.code.CDCZCostConfig;
import com.gzhryc.shared_device.cdcz.code.dao.db.CDCZFeeTemplate;
import com.gzhryc.shared_device.cdcz.code.dao.models.CDCZPowerIntervalMoneyContent;
import com.gzhryc.shared_device.cdcz.code.services.CDCZFeeTemplateService;
import com.gzhryc.shared_device.cdcz.code.services.dto.CDCZFeeTemplateSearch;
import com.gzhryc.shared_device.oem.code.ConfigUtils;
import com.gzhryc.shared_device.oem.code.business.EBusinessType;
import com.gzhryc.shared_device.oem.code.place.services.PlaceBusinessConfigService;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;

public class CDCZFeeTemplateController {

    @Route(value = { "/cdcz/fee_template/list.html" })
    public String list(HttpServletRequest request) {
        return "/cdcz/cost/fee_template/list.html";
    }

    @Route(value = "/cdcz/fee_template/list.data")
    public ListResult<CDCZFeeTemplate> listData(HttpServletRequest request) {
        ListResult<CDCZFeeTemplate> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        CDCZFeeTemplateSearch search = WebUtils.getParameter(request, CDCZFeeTemplateSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

        result.getBody().setTotal(CDCZFeeTemplateService.self().countJoinBySearch(search));
        List<CDCZFeeTemplate> feeTemplateList = CDCZFeeTemplateService.self().findJoinBySearch(search, pageNo, pageLimit);
        if (feeTemplateList != null && feeTemplateList.size() > 0) {
            result.setCode(DtoConstants.SUCCESS, null);
            result.getBody().setData(feeTemplateList);
        }
        return result;
    }

    @Route(value = { "/cdcz/fee_template/add.html" })
    public String add(HttpServletRequest request) {
        return "/cdcz/cost/fee_template/edit.html";
    }

    @Route(value = {"/cdcz/fee_template/add.do"})
    public AjaxResult doAdd(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "新增收费模版失败");
        CDCZFeeTemplate feeTemplate = WebUtils.getParameter(request, CDCZFeeTemplate.class);
        try {
            if(feeTemplate != null) {
                Double minWalletMoneyStr = WebUtils.getDouble(request,"minWalletMoneyStr");
                if(minWalletMoneyStr == null || minWalletMoneyStr < 0.01 || minWalletMoneyStr > 99){
                    result.setMsg("钱包最小余额设置范围在0.01-99元");
                    return result;
                }
                feeTemplate.setMinWalletMoney(NumberTools.changeMoneyLi(minWalletMoneyStr));

                CDCZCostConfig config = ConfigUtils.getCDCZCostConfig();
                if(StringTools.isNotBlank(config.getPimContent())) {
                    feeTemplate.setPimContent(config.getPimContent());
                }else {
                    result.setMsg("请在业务配置中设置默认的功率阶梯价格");
                    return result;
                }

                if (CDCZFeeTemplateService.self().add(feeTemplate)) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/cdcz/fee_template/update.html" })
    public String update(HttpServletRequest request) {
        Long id = WebUtils.getLong(request,"id");
        CDCZFeeTemplate feeTemplate = CDCZFeeTemplateService.self().getJoinById(id);
        WebUtils.setAttribute(request,feeTemplate);
        return "/cdcz/cost/fee_template/edit.html";
    }

    @Route(value = {"/cdcz/fee_template/update.do"})
    public AjaxResult doUpdate(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "修改收费模版失败");
        CDCZFeeTemplate feeTemplate = WebUtils.getParameter(request, CDCZFeeTemplate.class);
        try {
            if(feeTemplate != null) {
                Double minWalletMoneyStr = WebUtils.getDouble(request,"minWalletMoneyStr");
                if(minWalletMoneyStr == null || minWalletMoneyStr < 0.01 || minWalletMoneyStr > 99){
                    result.setMsg("钱包最小余额设置范围在0.01-99元");
                    return result;
                }
                feeTemplate.setMinWalletMoney(NumberTools.changeMoneyLi(minWalletMoneyStr));

                if (CDCZFeeTemplateService.self().updateNotNull(feeTemplate)) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = {"/cdcz/fee_template/del.do"})
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除收费模版失败");
        Long id = WebUtils.getLong(request,"id");
        Long total = PlaceBusinessConfigService.self().countByFeeTemplateId(EBusinessType.CDCZ.index(),id);
        if(total != null && total > 0){
            result.setMsg("该收费模版正在使用，不允许删除");
            return result;
        }

        try {
            if(CDCZFeeTemplateService.self().deleteById(id)){
                result.setCode(DtoConstants.SUCCESS, null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = "/cdcz/fee_template/power_interval/list.data")
    public ListResult<CDCZPowerIntervalMoneyContent.Item> powerInterval(HttpServletRequest request) {
        ListResult<CDCZPowerIntervalMoneyContent.Item> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        Long id = WebUtils.getLong(request,"id");
        CDCZFeeTemplate feeTemplate = CDCZFeeTemplateService.self().getById(id);
        if(feeTemplate != null){
            if(StringTools.isNotBlank(feeTemplate.getPimContent())) {
                CDCZPowerIntervalMoneyContent content = JsonTools.fromJson(feeTemplate.getPimContent(), CDCZPowerIntervalMoneyContent.class);
                if (content.getItems() != null && content.getItems().size() > 0) {
                    result.setCode(DtoConstants.SUCCESS, null);
                    result.getBody().setData(content.getItems());
                }
            }
        }else {
            CDCZCostConfig costConfig = ConfigUtils.getCDCZCostConfig();
            if(StringTools.isNotBlank(costConfig.getPimContent())) {
                CDCZPowerIntervalMoneyContent content = JsonTools.fromJson(costConfig.getPimContent(), CDCZPowerIntervalMoneyContent.class);
                if (content.getItems() != null && content.getItems().size() > 0) {
                    result.setCode(DtoConstants.SUCCESS, null);
                    result.getBody().setData(content.getItems());
                }
            }
        }
        return result;
    }

    @Route(value = { "/cdcz/fee_template/power_interval/add.html" })
    public String addSetMeal(HttpServletRequest request) {
        Long id = WebUtils.getLong(request,"id");
        CDCZFeeTemplate feeTemplate = CDCZFeeTemplateService.self().getById(id);
        if(feeTemplate != null && StringTools.isNotBlank(feeTemplate.getPimContent())) {
            CDCZPowerIntervalMoneyContent content = JsonTools.fromJson(feeTemplate.getPimContent(), CDCZPowerIntervalMoneyContent.class);
            if (content.getItems() != null && content.getItems().size() > 0) {
                CDCZPowerIntervalMoneyContent.Item item = content.getItems().get(content.getItems().size() - 1);
                request.setAttribute("minPower", item.getMaxPower());
            }
        }
        return "/cdcz/cost/fee_template/power_interval.html";
    }

    @Route(value = { "/cdcz/fee_template/power_interval/add.do" })
    public AjaxResult doAddSetMeal(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "新增功率区间收费失败");
        Long id = WebUtils.getLong(request,"id");
        CDCZPowerIntervalMoneyContent.Item item = WebUtils.getParameter(request, CDCZPowerIntervalMoneyContent.Item.class);
        if(id != null  && item != null && item.getMaxPower() != null && item.getMinPower() != null){
            Double moneyStr = WebUtils.getDouble(request,"moneyStr");
            if(moneyStr == null || moneyStr < 0.01 || moneyStr > 99){
                result.setMsg("功率区间金额设置范围在0.01-99元");
                return result;
            }
            if(item.getMaxPower() <= item.getMinPower()){
                result.setMsg("功率区间错误");
                return result;
            }
            item.setMoney(NumberTools.changeMoneyLi(moneyStr));

            try {
                CDCZFeeTemplate feeTemplate = CDCZFeeTemplateService.self().getById(id);
                if(CDCZFeeTemplateService.self().addPowerIntervalMoneyItem(feeTemplate,item)){
                    result.setCode(DtoConstants.SUCCESS,null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }

    @Route(value = { "/cdcz/fee_template/power_interval/update.html" })
    public String updateSetMeal(HttpServletRequest request) {
        Long id = WebUtils.getLong(request,"id");
        Double minPower = WebUtils.getDouble(request,"minPower");
        if(id != null) {
            CDCZFeeTemplate feeTemplate = CDCZFeeTemplateService.self().getById(id);
            if(feeTemplate != null && StringTools.isNotBlank(feeTemplate.getPimContent())) {
                CDCZPowerIntervalMoneyContent content = JsonTools.fromJson(feeTemplate.getPimContent(), CDCZPowerIntervalMoneyContent.class);
                if (content.getItems() != null && content.getItems().size() > 0) {
                    for (CDCZPowerIntervalMoneyContent.Item item : content.getItems()) {
                        if (item.getMinPower().equals(minPower)) {
                            WebUtils.setAttribute(request, item);
                            break;
                        }
                    }
                }
            }
        }
        return "/cdcz/cost/fee_template/power_interval.html";
    }

    @Route(value = { "/cdcz/fee_template/power_interval/update.do" })
    public AjaxResult doUpdateSetMeal(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "修改功率区间收费失败");
        Long id = WebUtils.getLong(request,"id");
        CDCZPowerIntervalMoneyContent.Item item = WebUtils.getParameter(request, CDCZPowerIntervalMoneyContent.Item.class);
        if(id != null  && item != null && item.getMaxPower() != null && item.getMinPower() != null) {
            Double moneySer = WebUtils.getDouble(request, "moneyStr");
            if(moneySer == null || moneySer < 0.01 || moneySer > 99){
                result.setMsg("功率区间金额设置范围在0.01-99元");
                return result;
            }
            if(item.getMaxPower() <= item.getMinPower()){
                result.setMsg("功率区间错误");
                return result;
            }
            item.setMoney(NumberTools.changeMoneyLi(moneySer));
            try {
                CDCZFeeTemplate feeTemplate = CDCZFeeTemplateService.self().getById(id);
                if(CDCZFeeTemplateService.self().updatePowerIntervalMoneyItem(feeTemplate,item)){
                    result.setCode(DtoConstants.SUCCESS,null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }

    @Route(value = { "/cdcz/fee_template/power_interval/del.do" })
    public AjaxResult doDelSetMeal(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除功率区间收费失败");
        Long id = WebUtils.getLong(request,"id");
        Double minPower = WebUtils.getDouble(request,"minPower");
        if(id != null && minPower != null) {
            try {
                CDCZFeeTemplate feeTemplate = CDCZFeeTemplateService.self().getById(id);
                if (CDCZFeeTemplateService.self().deletePowerIntervalMoneyItem(feeTemplate, minPower)) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }
}
